var systemInterfaceParam=null;
$(document).ready(function(){
		var urlParamObj = getParamobjFromUrl();
		//所属领域下拉框
		$('#comboxSearchfieldName').combobox({
			url: BASE_URL_FIELD_SEARCH + '/findAllOfCurrentUserCanManage'+ '?v=' + Math.random(),
			valueField: 'id',
		    textField: 'name',
		    editable: false,
		    method:'get',
		    loadFilter: function(data){
		    	var allCheck = {'id':'','name':'全部'};
		    	data.content.unshift(allCheck);
		    	return data.content;
		    },
		    onSelect: function(record){
		    	comboxSearchSystemName.combobox({
                  disabled: false,
                  method:'get',
                  queryParams: {fieldId: record.id},
                  url: BASE_URL_FIELD_SYSTEM_SEARCH +'/findByFieldOfCurrentUserCanManage'+ '?v=' + Math.random(),
                  valueField: 'id',
                  textField: 'name',
				  data:'',
      		      loadFilter: function(data){
      		    	var allCheck = {'id':'','name':'全部','selected':true};
      		    	data.content.unshift(allCheck);
      		    	return data.content;
      		    },
      		    onLoadSuccess:function(){
					if(urlParamObj.fieldSystemId)
					{
						comboxSearchSystemName.combobox('setValue',urlParamObj.fieldSystemName? decodeURI(urlParamObj.fieldSystemName):"");
						comboxSearchSystemName.combobox("select",urlParamObj.fieldSystemId);
						urlParamObj.fieldSystemId = '';
					}
					if(urlParamObj.interfaceName)
					{
						$("#txtSearchInterfaceName").val(decodeURI(urlParamObj.interfaceName));
					}
					if(urlParamObj.beginOfRunEndTime && urlParamObj.endOfRunEndTime)
					{
						 $("#txtBeginTime").datetimebox("setValue",decodeURI(urlParamObj.beginOfRunEndTime)); 
	 　 					 $("#txtEndTime").datetimebox("setValue",decodeURI(urlParamObj.endOfRunEndTime));
	 					 dataRetrieval('#txtBeginTime','#txtEndTime');
					}
					
      		    }
              }).load();
		    },
		    onLoadSuccess:function(){
				if(urlParamObj.fieldName){
					var datas = $('#comboxSearchfieldName').combobox('getData');
					for(var i=0;i< datas.length;i++){
						if(datas[i].name == decodeURI(urlParamObj.fieldName)){
							$('#comboxSearchfieldName').combobox('select',datas[i].id);
						}
					}
				}
		    }
		});
		
		//所属系统下拉框
		var comboxSearchSystemName = $('#comboxSearchSystemName').combobox({
          disabled: true,
          valueField: 'id',
          textField: 'name',
          editable: false,
		  data:[{id:'',name:'全部','selected':'true'}]
      });
		
		
		$("#datagrid").datagrid({
			//url : BASE_URL_HISTORY_INTERFACE_SEARCH + '/faildInterface' + '?v=' + Math.random(),
			method:'get',
			autoRowHeight:true,
			fitColumns:true,
			nowrap:false,
			striped:true,
			pageSize:20,
			pageList:[10,20,50,100],
			columns:[[
						{field:'fieldName',title:'领域',width:100},
						{field:'fieldSystemName',title:'系统',width:70},
						{field:'interfaceName',title:'接口名称',width:100,align:'left'},
						{field:'scheduleName',title:'调度名',width:100,align:'left'},
						{field:'runBeginTime',title:'执行开始时间',width:100},
						{field:'runEndTime',title:'执行结束时间',width:100},
						{field:'runResultForDisplay',title:'执行结果',width:100},
						{field:'operation',title:'操作',width:60,
							formatter: function(value,row,index){
								//alert("id:"+row.id);
								return '<a href="#" onclick="showLog(' + index+ ')">查看日志</a>';		
							}
						},						
						{field:'errSendTo',title:'邮件通知',width:140,align:'left'}
					]],
			pagination:true,
			onSelect:function(rowIndex,rowData){
				/*$("#toolbar_edit").linkbutton('enable');
				$("#toolbar_delete").linkbutton('enable');*/
			},
			loadFilter:function(data){
				//alert("inData:"+JSON.stringify(data));
				return {
					total: data.totalElements,
					rows: data.content
				};
			},
			onBeforeLoad:function(param){
				param.page -= 1;
				param.size = param.rows;
				delete param.rows;
				
				
				param.sort = 'createTime,desc';
				
				var fieldName = $('#comboxSearchfieldName').combobox('getText');
				if(fieldName != '全部'){
					//alert("getText:"+fieldName);
					if(fieldName !=""){
						param.fieldName = fieldName;
					}
				}
				if(param.fieldSystemName){
					param.fieldSystemName = urlParamObj.fieldSystemName;
				}else{
					var fieldSystemName = $('#comboxSearchSystemName').combobox('getText');
					if(fieldSystemName != '全部'){
						//alert("getText:"+fieldSystemName);
						if(fieldSystemName !=""){
							param.fieldSystemName = fieldSystemName;
						}
					}
				}
				if(param.interfaceName){
					param.interfaceName = urlParamObj.interfaceName;
				}else{
					var interfaceName = $('#txtSearchInterfaceName').val();
					if(interfaceName !=""){
						//alert(jobName);
						param.interfaceName = interfaceName;
					}
				}
				if(param.beginOfCreateTime){
					param.beginOfCreateTime = urlParamObj.beginOfRunEndTime;
				}else{
					var beginOfCreateTime = $('#txtBeginTime').datetimebox('getValue');
					if(beginOfCreateTime !=""){
						param.beginOfCreateTime = beginOfCreateTime;
					}
				}
				if(param.endOfCreateTime){
					param.endOfCreateTime = urlParamObj.endOfCreateTime;
				}else{
				
					var endOfCreateTime = $('#txtEndTime').datetimebox('getValue');
					if(endOfCreateTime !=""){
						param.endOfCreateTime = endOfCreateTime;
					}
				}
				systemInterfaceParam  = param;
			}
		});
		
		$("#datagrid").datagrid('getPager').pagination({   
		    displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录'
		}); 


	//查看日志
	$('#tips_log').window({
		width:550,
		modal:true,
		minimizable:false,
		maximizable:false,
		collapsible:false
	});
	$('#tips_log').window('close');  //关闭窗口
});
	//查询
	function search(){
		var opts = $("#datagrid").datagrid("options");
	    opts.url = BASE_URL_HISTORY_INTERFACE_SEARCH + '/faildInterface' + '?v=' + Math.random();
		dataRetrieval('#txtBeginTime','#txtEndTime');
	}
	
	//查看日志操作
	function showLog(index)
	{
		$('#datagrid').datagrid('selectRow',index);
		var row = $('#datagrid').datagrid('getSelected');
				
		if(row == null){
			$.messager.alert("提示","<p>请选中要查看的记录!</p>","info");
		}else{
			$.ajax({
				url:BASE_URL_HISTORY_INTERFACE_SEARCH+ '/logs' + "?id=" + row.id + '&v=' + Math.random(),
				dataType:'json',
				type:'get',
				contentType:"application/json",
				success:function(data){
					if(data.logField !== null)
					{
						var logArr = [];
						var html = '';
						var logField = data.logField;
						logArr = logField.split("&lt;br&gt;");
						for(var i = 0, len = logArr.length; i < len; i++)
						{
							if(i % 2 == 0)
							{
								if(logArr[i] === "null")
								{
									html += '<p class="even"><span>' + logArr[i] + '</span></p>';
								}else
								{
									html += '<p class="even"><strong>' + logArr[i].split(" - ")[0] + '</strong><br /><span>' +logArr[i].split(" - ")[1] + " - " + logArr[i].split(" - ")[2] + '</span></p>';
								}
							}else
							{
								if(logArr[i] === "null")
								{
									html += '<p class="odd"><span>' + logArr[i] + '</span></p>';
								}else
								{
									html += '<p class="odd"><strong>' + logArr[i].split(" - ")[0] + '</strong><br /><span>' + logArr[i].split(" - ")[1] + " - " +  logArr[i].split(" - ")[2] + '</span></p>';
								}
							}
						}
						//fieldSystemName.replace("\r\n", "<br />");			
						$("#logTxt").html(html);
						$('#tips_log').window('open');
					}else
					{
						$.messager.alert("提示","<p>日志记录为空！</p>","info",function(e)
						{
								$("#datagrid").datagrid("reload", BASE_URL_HISTORY_INTERFACE_SEARCH + '/faildInterface' + '?v=' + Math.random());
						});
						
					}
				}
			});
			
			 
		}		
	}
	//导出报表操作
	function report()
	{
		$('#datagrid').datagrid({
			url:BASE_URL_HISTORY_INTERFACE_SEARCH + '/faildInterface' + '?v=' + Math.random()
		});
		var param = JSON.stringify(systemInterfaceParam);
		location.href= BASE_URL_EXPORTDATA_FILE + '/failInterfaceScheduleExport?param='+param;
		
	}